'use strict';

const Controller = require('egg').Controller;

class BorrowController extends Controller {
    //借书
    async create(){
      const{ctx,service}=this;
      const payload=ctx.request.body ||{};
      const res=await service.borrow.create(payload);

      ctx.body={
        code:200,
        data:res
      }
    }
//查看借书情况
    async findborrow() {
        const {ctx,service}=this;
        const name=ctx.query;//获取路由参数
        console.log(name);
        const res=await service.borrow.find(name);
        if(res&&res!=""){
          ctx.body={
            code:200,
            data:res
          }
        }else{
          ctx.body={
            data:'没有',
          }
        }
      }
//归还图书
    async delete(){
      const{ctx,service}=this;
      const{_id}=ctx.query;
      console.log(_id);
      const res= await service.borrow.delete(_id);
      if(res=="用户不存在或者未输入ID"){
        var code=5;
        ctx.body={res,data:{code}};
      }else{
        var code=500;
        ctx.body={res,data:{code}};
      }
    }
//书名查找
  async findname() {
    const {ctx,service}=this;
    const bookname=ctx.query;//获取路由参数
    console.log(bookname);
    const res=await service.borrow.findname(bookname);
    if(res&&res!=""){
      ctx.body={
        code:200,
        data:res
      }
    }else{
      ctx.body={
        data:'没有',
      }
    }
  }
  async f(){
    const {ctx,service}=this;
    const _id=ctx.query;
    const res =await service.borrow.f(_id);
    if(res&&res!=""){
      ctx.body={
        code:200,
        data:res
      }
    }else{
      ctx.body={
        data:'没有',
      }
    }
  }
}

module.exports = BorrowController;
